package com.docmanage.am.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.lang.func.VoidFunc0;
import com.docmanage.am.domain.dto.DirectoryRoleSaveDTO;
import com.docmanage.am.domain.dto.RoleDirectorySaveDTO;
import org.springframework.stereotype.Service;
import com.docmanage.am.mapper.AmRoleDirectoryMapper;
import com.docmanage.am.domain.AmRoleDirectory;

import javax.annotation.Resource;

/**
 * 目录角色关联Service业务层处理
 * 
 * @author docmanage
 * @date 2024-03-07
 */
@Service
public class AmRoleDirectoryServiceImpl
{
    @Resource
    private AmRoleDirectoryMapper amRoleDirectoryMapper;



    /**
     * 查询目录角色关联列表
     * 
     * @param amRoleDirectory 目录角色关联
     * @return 目录角色关联
     */

    public List<AmRoleDirectory> selectAmRoleDirectoryList(AmRoleDirectory amRoleDirectory)
    {
        return amRoleDirectoryMapper.selectAmRoleDirectoryList(amRoleDirectory);
    }



    /**
     * 新增目录角色关联
     * 
     * @param dto 目录角色关联
     * @return 结果
     */

    public int insertAmRoleDirectory(RoleDirectorySaveDTO dto)
    {
        //删除对应角色下的目录分配数据
        deleteAmRoleDirectoryById(dto.getRoleId());
        //新增
        dto.getDirectoryIds().forEach(item->{
            AmRoleDirectory amRoleDirectory = new AmRoleDirectory();
            amRoleDirectory.setRoleId(dto.getRoleId());
            amRoleDirectory.setDirectoryId(item);
            amRoleDirectoryMapper.insertAmRoleDirectory(amRoleDirectory);
        });

        return 1;
    }


    /**
     * 删除目录角色关联信息
     * 
     * @param roleId 目录角色关联角色id
     * @return 结果
     */
    public int deleteAmRoleDirectoryById(Long roleId)
    {
        return amRoleDirectoryMapper.deleteAmRoleDirectoryById(roleId);
    }

    public List<Long> getDirectoryId(Long roleId) {
        List<Long> list=new ArrayList<>();
        AmRoleDirectory amRoleDirectory = new AmRoleDirectory();
        amRoleDirectory.setRoleId(roleId);
        List<AmRoleDirectory> amRoleDirectories = selectAmRoleDirectoryList(amRoleDirectory);
        amRoleDirectories.forEach(item->{
            list.add(item.getDirectoryId());
        });
        return list;
    }


    /**
     * 新增目录角色关联
     *
     * @param dto 目录角色关联
     * @return 结果
     */

    public int insertAmDirectoryRole(DirectoryRoleSaveDTO dto)
    {
        //删除对应角色下的目录分配数据
        deleteAmRoleDirectoryByDirId(dto.getDirectoryId());
        //新增
        dto.getRoleIds().forEach(item -> {
            AmRoleDirectory amRoleDirectory = new AmRoleDirectory();
            amRoleDirectory.setRoleId(item);
            amRoleDirectory.setDirectoryId(dto.getDirectoryId());
            amRoleDirectoryMapper.insertAmRoleDirectory(amRoleDirectory);
        });

        return 1;
    }

    /**
     * 删除目录角色关联信息
     *
     * @param directoryId 目录角色关联角色id
     * @return 结果
     */
    public int deleteAmRoleDirectoryByDirId(Long directoryId)
    {
        return amRoleDirectoryMapper.deleteAmRoleDirectoryByDirId(directoryId);
    }

    public List<Long> getRoleIds(Long directoryId) {
        List<Long> list=new ArrayList<>();

        AmRoleDirectory amRoleDirectory = new AmRoleDirectory();
        amRoleDirectory.setDirectoryId(directoryId);

        List<AmRoleDirectory> amRoleDirectories = selectAmRoleDirectoryList(amRoleDirectory);
        amRoleDirectories.forEach(item->{
            list.add(item.getRoleId());
        });
        return list;
    }
}
